package ywpt.service.client;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.directwebremoting.annotations.RemoteMethod;
import org.directwebremoting.annotations.RemoteProxy;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;

import ywpt.core.dao.HibernateEntityDao;
import ywpt.model.ClientInfo;
import ywpt.model.ClientKeshi;
import ywpt.model.ClientLinkman;
import ywpt.model.ClientSoft;
import ywpt.model.CommonTable;
import ywpt.model.ProviderInfo;
import ywpt.web.common.PublicFunction;
@Service("clientSoftService")
@RemoteProxy(name="clientSoftService")
public class ClientSoftServiceImpl extends HibernateEntityDao<ClientSoft> implements ClientSoftService{
	public ClientSoftServiceImpl(){
		
	}

	@Override
	public String createSoftCode() {
		DetachedCriteria query = DetachedCriteria.forClass(ClientSoft.class, "soft");
		String softCode="";
		String code="";
		Integer maxId=new Integer(0);
		query.addOrder(Order.desc("id"));
		List<ClientSoft> clientSofts = this.getClientSofts(query);
		if ( clientSofts.iterator().hasNext() )	
		{
			code = clientSofts.iterator().next().getCode();
			maxId = new Integer(code);
		}
		softCode=PublicFunction.getNextCode("", maxId, false,4); 
		query.setProjection(null);
		return softCode;
	}

	@Override
	@RemoteMethod
	public ClientSoft getClientSoftById(Long id) {
		return this.get(id);
	}

	@Override
	public int getClientSoftCountByCriteria(DetachedCriteria query) {
		return this.getCountByCriteria(query);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ClientSoft> getClientSofts(DetachedCriteria query,
			int pageSize, int startIndex) {
		return this.findAllByCriteria(query, pageSize, startIndex);
	}

	@Override
	public List<ClientSoft> getClientSofts() {
		return this.getAll();
	}

	@Override
	public void removeClientSoft(Long id) {
		super.removeById(id);
	}

	@Override
	public void saveClientSoft(ClientSoft clientSoft, Long clientId,
			Long keshiId, Long providerId, Long linkmanId,Long natureId,Long stateId) {
		if(clientId != null){
			clientSoft.setClient(this.get(ClientInfo.class,clientId));
		}
		if(keshiId != null){
			clientSoft.setKeshi(this.get(ClientKeshi.class, keshiId));
		}
		if(providerId != null){
			clientSoft.setProvider(this.get(ProviderInfo.class, providerId));
		}
		if(linkmanId != null){
			clientSoft.setLinkman(this.get(ClientLinkman.class, linkmanId));
		}
		if (natureId != null) {
			clientSoft.setNature(this.get(CommonTable.class, natureId));
		}
		if (stateId != null) {
			clientSoft.setState(this.get(CommonTable.class, stateId));
		}
		super.save(clientSoft);
	}

	@Override
	public void updateClientSoft(ClientSoft clientSoft, Long clientId,
			Long keshiId, Long providerId, Long linkmanId,Long natureId,Long stateId) {
		if(clientId != null){
			clientSoft.setClient(this.get(ClientInfo.class,clientId));
		}
		if(keshiId != null){
			clientSoft.setKeshi(this.get(ClientKeshi.class, keshiId));
		}
		if(providerId != null){
			clientSoft.setProvider(this.get(ProviderInfo.class, providerId));
		}
		if(linkmanId != null){
			clientSoft.setLinkman(this.get(ClientLinkman.class, linkmanId));
		}
		if (natureId != null) {
			clientSoft.setNature(this.get(CommonTable.class, natureId));
		}
		if (stateId != null) {
			clientSoft.setState(this.get(CommonTable.class, stateId));
		}
		super.update(clientSoft);
		
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ClientSoft> getClientSofts(DetachedCriteria query) {
		return this.findAllByCriteria(query);
	}

	@SuppressWarnings("unchecked")
	@Override
	@RemoteMethod
	public List<ClientSoft> getSoftBySoftName(String softName,Long clientId, int start,
			int size) {
		DetachedCriteria query = DetachedCriteria.forClass(ClientSoft.class, "soft");
		if (clientId!=null) {
			query.createAlias("client", "client").add(Restrictions.eq("client.id", clientId));
		}
		//query.add(Restrictions.eq("organType", "01"));
		if ( softName != null && !softName.trim().equals("") )
		{
			query.add(Restrictions.or(Restrictions.like("softName", "%" + softName + "%"),  
							          Restrictions.like("code", "%" + softName + "%")));
				// .add(Restrictions.eq("organType", "01"));
		}
		query.addOrder(Order.asc("code"));
		List<ClientSoft> clientSoftList = new ArrayList<ClientSoft>();
		List<ClientSoft> clientSoftLists = new ArrayList<ClientSoft>();
		ClientSoft clientSoft =new ClientSoft();
		
		clientSoftList.addAll(this.findAllByCriteria(query, size, start));
		int i = 1;
		for ( Iterator<ClientSoft> itr = clientSoftList.iterator(); itr.hasNext(); i++ ) 
		{
			clientSoft = itr.next();
			String ss = PublicFunction.format(clientSoft.getCode() + "", 7, "  ", 1)
			          //+ PublicFunction.format(organ.getOrganName() + "", 14, "  ", 1);
			          + clientSoft.getSoftName(); 
			
			clientSoft.setSoftName(ss);
			clientSoftLists.add(clientSoft);
		}
		return clientSoftLists;
	}

	@Override
	@RemoteMethod
	public int getSoftCountByName(String softName,Long clientId) {
		DetachedCriteria query = DetachedCriteria.forClass(ClientSoft.class, "soft");
		if (clientId!=null) {
			query.createAlias("client", "client").add(Restrictions.eq("client.id", clientId));
		}
		//query.add(Restrictions.eq("organType", "01"));
		if ( softName != null && !softName.trim().equals("") )
		{
			query.add(Restrictions.or(Restrictions.like("softName", "%" + softName + "%" ),  
							          Restrictions.like("code", "%" + softName + "%" )));
				 //.add(Restrictions.eq("organType", "01"));
		}
		query.setProjection(null);
		return this.getCountByCriteria(query);
	}
}
